﻿using UnityEditor;
using UnityEngine;
using HKTools;

namespace HKTools
{
    /// <summary>
    /// HK_BezierLineRenderer 的自定义编辑器，用于场景视图交互和参数调整
    /// </summary>
    [CustomEditor(typeof(HK_BezierLineRenderer))]
    public class HK_BezierLineRendererEditor : Editor
    {
        HK_BezierLineRenderer targetBezier;

        void Awake()
        {
            targetBezier = target as HK_BezierLineRenderer;
        }

        void OnSceneGUI()
        {
            if (targetBezier == null) return;

            #region 坐标计算（与核心组件逻辑同步）
            Vector3 startWorldPos = targetBezier.isWorldSpace
                ? targetBezier.startPoint
                : targetBezier.transform.TransformPoint(targetBezier.startPoint);
            Vector3 endWorldPos = targetBezier.isWorldSpace
                ? targetBezier.endPoint
                : targetBezier.transform.TransformPoint(targetBezier.endPoint);

            Quaternion baseRotation = targetBezier.isWorldSpace ? targetBezier.transform.rotation : Quaternion.identity;
            Vector3 startControlDir = (baseRotation * targetBezier.startControlRotation) * Vector3.forward;
            Vector3 endControlDir = (baseRotation * targetBezier.endControlRotation) * Vector3.forward;
            Vector3 controlPoint1 = startWorldPos + startControlDir * targetBezier.startHandleLength;
            Vector3 controlPoint2 = endWorldPos + endControlDir * targetBezier.endHandleLength;
            #endregion

            #region 控制柄辅助线绘制
            Handles.color = Color.cyan;
            Handles.DrawDottedLine(startWorldPos, controlPoint1, 5f);
            Handles.Label(controlPoint1 + Vector3.up * 0.2f, $"Start Handle\nLength: {targetBezier.startHandleLength:F2}");

            Handles.color = Color.magenta;
            Handles.DrawDottedLine(endWorldPos, controlPoint2, 5f);
            Handles.Label(controlPoint2 + Vector3.up * 0.2f, $"End Handle\nLength: {targetBezier.endHandleLength:F2}");
            #endregion

            #region 控制柄长度调整（黄色滑动手柄）
            Handles.color = Color.yellow;
            EditorGUI.BeginChangeCheck();
            float handleSize = HandleUtility.GetHandleSize(controlPoint1) * 0.3f; // 随视角动态调整手柄大小
            Vector3 newControlPoint1 = Handles.Slider(controlPoint1, startControlDir, handleSize, Handles.ConeHandleCap, 0f);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Adjust Start Handle Length");
                targetBezier.startHandleLength = Vector3.Distance(startWorldPos, newControlPoint1);
                targetBezier.UpdateBezierCurve();
            }

            Handles.color = Color.yellow;
            EditorGUI.BeginChangeCheck();
            handleSize = HandleUtility.GetHandleSize(controlPoint2) * 0.3f;
            Vector3 newControlPoint2 = Handles.Slider(controlPoint2, endControlDir, handleSize, Handles.ConeHandleCap, 0f);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Adjust End Handle Length");
                targetBezier.endHandleLength = Vector3.Distance(endWorldPos, newControlPoint2);
                targetBezier.UpdateBezierCurve();
            }
            #endregion

            #region 端点位置调整（绿色/红色位置手柄）
            Handles.color = Color.green;
            EditorGUI.BeginChangeCheck();
            Vector3 newStartWorldPos = Handles.PositionHandle(startWorldPos, Quaternion.identity);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Move Start Point");
                targetBezier.startPoint = targetBezier.isWorldSpace
                    ? newStartWorldPos
                    : targetBezier.transform.InverseTransformPoint(newStartWorldPos);
                targetBezier.UpdateBezierCurve();
            }

            Handles.color = Color.red;
            EditorGUI.BeginChangeCheck();
            Vector3 newEndWorldPos = Handles.PositionHandle(endWorldPos, Quaternion.identity);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Move End Point");
                targetBezier.endPoint = targetBezier.isWorldSpace
                    ? newEndWorldPos
                    : targetBezier.transform.InverseTransformPoint(newEndWorldPos);
                targetBezier.UpdateBezierCurve();
            }
            #endregion

            #region 控制柄旋转调整（绿色/红色旋转手柄）
            Handles.color = Color.green;
            EditorGUI.BeginChangeCheck();
            Quaternion newStartRot = Handles.RotationHandle(targetBezier.startControlRotation, startWorldPos);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Rotate Start Control Handle");
                targetBezier.startControlRotation = newStartRot;
                targetBezier.UpdateBezierCurve();
            }

            Handles.color = Color.red;
            EditorGUI.BeginChangeCheck();
            Quaternion newEndRot = Handles.RotationHandle(targetBezier.endControlRotation, endWorldPos);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(targetBezier, "Rotate End Control Handle");
                targetBezier.endControlRotation = newEndRot;
                targetBezier.UpdateBezierCurve();
            }
            #endregion

            #region 句柄标签绘制
            Handles.Label(startWorldPos + Vector3.up * 0.2f, "Start Point (Green)");
            Handles.Label(endWorldPos + Vector3.up * 0.2f, "End Point (Red)");
            #endregion
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            if (GUI.changed)
            {
                targetBezier?.UpdateBezierCurve(); // 空值保护
            }
        }
    }
}